﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Informaiton_Retrieval.com.ir.utli;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Informaiton_Retrieval.com.ir.Model
{
    class MainModel
    {
        VeryLargeFile file;
      public  PresentationModel pre;

        public MainModel(PresentationModel presentation)
        {
            file = new VeryLargeFile(this);
            this.pre = presentation;

        }

        public void LoadFile(string filePath)
        {
            file = new VeryLargeFile(this);
            Task.Factory.StartNew(() =>
            {
                Load(filePath);
            });

        }

        private void Load(string path)
        {
            pre.SetIsFileProcessCompleted(false);
            pre.SetIsFileInProcess(true);
            file.CalculateLines(path);
            file.LoadData(path);
            pre.SetIsFileInProcess(false);
            OnFileLoadComplete();
        }

        public void SaveFile(string path) {
            Task.Factory.StartNew(() =>
            {
                Save(path);
            });
        }

        private void Save(string path)
        {
            pre.SetIsFileProcessCompleted(false);
            pre.SetIsFileInProcess(true);
            file.SaveOutput(path);
            pre.SetIsFileInProcess(false);
            OnFileSaveComplete();
        }

        public void LoadFileProgress(int percent){
            pre.OnSetProgress(percent);
        }

        public void LoadedLine(string line) {
            pre.OnSetMeaasge(line);
        }

        private void OnFileLoadComplete() {
            MessageBox.Show("data index done!");
            pre.SetIsFileProcessCompleted(true);
        }

        private void OnFileSaveComplete()
        {
            MessageBox.Show("data output done!");
            pre.SetIsFileProcessCompleted(true);
        }

    }
}
